	var setting = {

		view: {
			dblClickExpand: false,
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		edit: {
			enable: true,
			editNameSelectAll: true,
			showRemoveBtn: false,
			showRenameBtn: false,
		},
		data: {
			simpleData: {
				enable: true,
			}
		},
		callback: {
			beforeDrag: beforeDrag
		}
	};

	function beforeDrag(treeId, treeNodes) {
		return false;
	}


	function addHoverDom(treeId, treeNode) {
		
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0 || $("#editBtn_"+treeNode.tId).length>0 || $("#delBtn_"+treeNode.tId).length>0) return;

		var addStr = "<span class='glyphicon glyphicon-plus' id='addBtn_" + treeNode.tId
		+ "' title='新增' style='font-size:14px;vertical-align:top' onfocus='this.blur();'></span>";
		addStr += "<span class='glyphicon glyphicon-pencil' id='editBtn_" + treeNode.tId
		+ "' title='修改' style='font-size:14px;vertical-align:top' onfocus='this.blur();'></span>";
		if(treeNode.level > 0){
			addStr += "<span class='glyphicon glyphicon-trash' id='delBtn_" + treeNode.tId
			+ "' title='删除' style='font-size:14px;vertical-align:top' onfocus='this.blur();'></span>";
		}

		
		
		sObj.after(addStr);
		var addbtn = $("#addBtn_"+treeNode.tId);
		var editbtn = $("#editBtn_"+treeNode.tId);
		var delbtn = $("#delBtn_"+treeNode.tId);
		
		if (addbtn) addbtn.bind("click", function(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);				
			//这里是自定义事件();
			$("#channelForm").validate().resetForm();//清除validate状态
			$('#myModal').modal({backdrop: 'static', keyboard: false});//模态框禁用空白处点击关闭
			$("#myModalLabel").text("新增频道");
			$('#myModal').modal('show');
			$('#channelid').val("");
			$('#parentid').val(treeNode.id);
			$("#channelname").removeAttr("disabled");
			$('#channelname').val("");
			$('#channelshowname').val("");
			$("#channelurl").removeAttr("disabled");
			$('#channelurl').val("");
			$('#channelabsurl').val("");
			$('#channelabsurldiv').hide();
			$('#gselect').val("");
			$('#dselect').val("");
			return false;
		});
		
		if (editbtn) editbtn.bind("click", function(){
			//这里是自定义事件();
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
		    $.ajax({
		    	   url:"site!editchannel.action",   
				   type:"post",   
				   data:{id:treeNode.id},
				   async:false,
				   dataType:"json",   
				   success:function(data){
					    $('#myModal').modal({backdrop: 'static', keyboard: false});//模态框禁用空白处点击关闭
					    $("#myModalLabel").text("修改频道");
					    $('#myModal').modal('show');
					    $('#channelid').val(data.channelid);
						$('#parentid').val(data.parentid);
						$('#channelname').val(data.channelname);
						$("#channelname").attr('disabled','disabled');
						$('#channelshowname').val(data.channelshowname);
						$('#channelurl').val(data.channelurl);
						$("#channelurl").attr('disabled','disabled');
						if(treeNode.level > 0){
							$('#channelabsurldiv').hide();
						}else{
							$('#channelabsurldiv').show();
							$('#channelabsurl').val(data.channelabsurl);
						}
						$('#gselect').val(data.gselect);
						$('#dselect').val(data.dselect);
				   }
		    });
			return false;
		});
		
		if (delbtn) delbtn.bind("click", function(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			if(confirm("确认删除 节点 -- " + treeNode.name + " 吗？")){
			    $.ajax({
			    	   url:"site!delchannel.action",   
					   type:"post",   
					   data:{id:treeNode.id},
					   async:false,
					   dataType:"json",   
					   success:refresh
			    });
			}else{
				 var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				 zTree.cancelSelectedNode();
			}
			return false;
		});
		
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_"+treeNode.tId).unbind().remove();
		$("#editBtn_"+treeNode.tId).unbind().remove();
		$("#delBtn_"+treeNode.tId).unbind().remove();
	};
	
	 $("#btn_submit").click(function(){
		 
	    var flag = $("#channelForm").valid();
	    if(!flag){
	        //没有通过验证
	        return;
	    }
	  //取出disabled
	  $("#channelname").removeAttr("disabled");
	  $("#channelurl").removeAttr("disabled");
	  $('#channelabsurldiv').show();
	  //把表单的数据进行序列化   
	  var params = $("form").serialize();   
 
	  $.ajax({   
	   url:"site!addchannel.action",   
	   type:"post",   
	   data:params,
	   async:false,
	   dataType:"json",   
	   success:function(data){
		   refresh();
		   //关闭
		   $('#myModal').modal("hide");
	   }
	   });
	});
	 
	 $("#btn_cancel").click(function(){   
		 var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		 zTree.cancelSelectedNode();
	}); 
	
	 $("#btn_close").click(function(){   
		 var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		 zTree.cancelSelectedNode();
	});  
	 
	//初始化绑定模板
	$(function(){
		refresh();
		//填充select
		select();
		//初始隐藏
		$("#channelabsurldiv").hide();
		//绑定校验
		//validate配置
		//除了模板其他都不为空，新增时，频道名称和相对地址不能重复；修改时即存在channelid则不判断重复
	    $("#channelForm").validate({
	        rules: {
	        	channelname: {
	                //异步验证 开始
	                remote: {
	                    url: "site!validatechannelname.action",//发送请求的url地址
	                    type: "post", //请求方式
	                    dataType: "json",//接收的数据类型
	                    data: {
	                    	channelname: function () {
	                            return $("#channelname").val();
	                        }
	                    },
	                    dataFilter: function (data, type) { //过滤返回结果
	                        //增加对修改的忽略
	                    	var json = JSON.parse(data);
	                    	return json.result;
	                          
	                    }
	                }
	                //异步验证 结束
	            },
	            channelurl: {
	                //异步验证 开始
	                remote: {
	                    url: "site!validatechannelurl.action",//发送请求的url地址
	                    type: "post", //请求方式
	                    dataType: "json",//接收的数据类型
	                    data: {
	                    	channelurl: function () {
	                            return $("#channelurl").val();
	                        }
	                    },
	                    dataFilter: function (data, type) { //过滤返回结果
//	                    	if($("#channelid").val()!=""){
//	                    		return true;
//	                    	}
	                    	var json = JSON.parse(data);
	                    	return json.result;
	                    }
	                }
	                //异步验证 结束
	            }
	        },
	    	
	    	messages: {
	        	channelname: {
	                required: "请输入频道名称",
	                remote:"输入的频道名称已经存在"
	            },
	            channelurl: {
	                required: "请输入频道相对路径",
	                remote:"输入的频道相对路径已经存在"
	            },
	            channelabsurl: {
	                required: "请输入频道完整路径",
	            }
	        }
	    });
	});
	
	function refresh(){
		$.ajax({
			url : "site!listtreenodes.action",
			type : "post",
			async: false,
			dataType : "json",
			success : initZtree
		});
	}
	function initZtree(json) {
		var data = (json.tree);
		var zNodes = eval("(" + data + ")");
		zTree = $.fn.zTree.init($('#treeDemo'), setting, zNodes);
	}
	
	//填充select
	function select(){
		  $.ajax({   
			   url:"template!alltemplate.action",   
			   type:"post",   
			   async:false,
			   dataType:"json",   
			   success: function(data, status) {
			      $.each(data.alllist, function(index, item) {
			        $("#gselect").append(  //此处向select中循环绑定数据
			        "<option value="+item.templateid+">" + item.templatename+ "</option>");
		            $("#dselect").append(  //此处向select中循环绑定数据
		            "<option value="+item.templateid+">" + item.templatename+ "</option>");
			      });
			    },
			   error: function (xhr, textStatus, errorThrown) {
			        /*错误信息处理*/
					alert("进入error---");
					alert("状态码："+xhr.status);
					alert("状态:"+xhr.readyState);//当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
					alert("错误信息:"+xhr.statusText );
					alert("返回响应信息："+xhr.responseText );//这里是详细的信息
					alert("请求状态："+textStatus);
					alert(errorThrown);
					alert("请求失败"); 
						}
				   });  
	}
	
	